<!-- deprecated -->
{% include message/component/select_topic_dialog.html %}

<!-- 文件上传的组件 -->
{% include message/component/message_upload.html %}

{% init tag = "todo" %}
{% init show_input_box = True %}

{% if show_input_box %}
    <div class="card">
        <!-- 编辑器 -->
        <div class="card">
            {% if tag == "key" %}
                <textarea class="col-md-12 input-box" placeholder="保存话题/标签/关键字"></textarea>
            {% else %}
                <textarea class="col-md-12 input-box" placeholder="记录发生的事情/产生的想法..."></textarea>
            {% end %}

            <input type="file" id="filePicker" class="hide" multiple/>

            <input type="button" name="" class="send-button btn btn-default select-topic-btn" value="#话题">
            <input type="button" id="filePickerBtn" class="send-button btn btn-default" value="添加附件"/>
            <input type="button" id="createTodoBtn" class="send-button btn" value="创建">
        </div>
    </div>
{% end %}

<div class="col-md-12" id="todoItemList">
    <!-- 待办任务 AJAX加载 -->
</div>

<script type="text/javascript">
    $(function () {

        function loadTodoList() {
            var page = getUrlParam("page");
            if (page == undefined) {
                page = 1;
            }

            var params = {
                tag: "{{tag}}",
                format: "html",
                page: page,
            };
            $.get("/message/list", params, function (resp) {
                // console.log(resp);
                $("#todoItemList").html(resp);
            }).fail(function (e) {
                console.error(e);
                xnote.alert("调用接口失败，请重试");
            });
        }

        function onFileUploaded(event) {
            var inputText = event.target;
            var oldText = $(".input-box").val();
            var newText = oldText + inputText;
            $(".input-box").val(newText);
        }

        function onMessageReceived(event) {
            var data = event.data;
            if (data == "message.updated") {
                loadTodoList();
                xnote.closeAllDialog();
            }
        }

        function onMessageRefresh(event) {
            loadTodoList();
        }

        function updateMessageTag(id, tag) {
            $.post("/message/tag", {id: id, tag: tag}, function (resp) {
                if (resp.code == "success") {
                    loadTodoList();
                } else {
                    alert(resp.message);
                }
            });
        }

        $("#createTodoBtn").click(function () {
            var content = $(".input-box").val();
            $.post("/message/save", 
                {
                    content:content, 
                    tag: "{{tag}}"
                },
                function (respText) {
                    var data = respText;
                    if (data.code != "success") {
                        xnote.alert(data.message);
                    } else {
                        $(".input-box").val("");
                    }

                    loadTodoList();
            }).fail(function (data) {
                console.log(data);
                xnote.alert("创建TODO失败，请稍后重试");
            })
        });

        $("#todoItemList").on("click", ".todo-delete-btn", function (e) {
            var target = e.target;
            var id = $(target).attr("data-id");
            var content = $(target).attr("data-content");

            xnote.confirm("确认删除 '%s' ?".format(content), function (result) {
                $.post("/message/delete", {id: id}, function (resp) {
                    if (resp.code == "success") {
                        // 重新刷新页面
                        loadTodoList();
                    } else {
                        xnote.alert(data.message);
                    }
                });                    
            });
        });


        $("#todoItemList").on("click", ".todo-checkbox", function (e) {
            // todo完成的声音
            xnote.playAudio("todo_done");

            // 标记为完成
            var id = $(e.target).attr("data-id");
            updateMessageTag(id, "done");
        });



        // 监听上传事件
        xnote.on("message.upload", onFileUploaded);
        xnote.on("message.refresh", onMessageRefresh);

        // 加载todo列表
        loadTodoList();

        // 加载音频文件
        xnote.loadAudio("todo_done", "/static/audio/todo_done.mp3");

        window.addEventListener("message", onMessageReceived, false);
    });
</script>

